﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace HiddenFileManager
{

    /// <summary>
    /// File operations using .Net Data Protection API
    /// </summary>
    public static class ProtectedFile
    {
        static readonly byte[] _entropy = Encoding.UTF8.GetBytes("ProtectedFile");

        public static String[] ReadAllLines(String path)
        {
            String text = ReadAllText(path);
            return text.Split(new String[] {Environment.NewLine}, StringSplitOptions.None);            
        }

        public static void WriteAllLines(String path, String[] lines)
        {
            String text = String.Join(Environment.NewLine, lines);
            WriteAllText(path, text);
        }

        public static String ReadAllText(String path)
        {
            byte[] encData = File.ReadAllBytes(path);
            byte[] data = ProtectedData.Unprotect(encData, _entropy, DataProtectionScope.CurrentUser);
            return Encoding.UTF8.GetString(data);
        }

        public static void WriteAllText(String path, String text)
        {
            byte[] data = Encoding.UTF8.GetBytes(text);
            byte[] encData = ProtectedData.Protect(data, _entropy, DataProtectionScope.CurrentUser);
            File.WriteAllBytes(path, encData);
        }
    }
}
